library(foreign)
library(iotools)

#This script uses the output from eba_bsc_A.py

#Set working directory
#setwd()
dir.path <- '/eba.out.bscA/'

deci <- function(x, k, center = TRUE) {
	a <- format(round(x, k), nsmall=k)
	return(a)
}

#----------------------------------------------------
#Figure 5
#----------------------------------------------------

failed.bsc_0 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_0_0.csv', sep = ''))
failed.bsc_1 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_1_0.csv', sep = ''))
failed.bsc_2 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_2_0.csv', sep = ''))
failed.bsc_3 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_3_0.csv', sep = ''))
failed.bsc_4 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_4_0.csv', sep = ''))
failed.bsc_5 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_5_0.csv', sep = ''))
failed.bsc_6 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_6_0.csv', sep = ''))

failed.bsc <- rbind(
	failed.bsc_0,
	failed.bsc_1,
	failed.bsc_2,
	failed.bsc_3,
	failed.bsc_4,
	failed.bsc_5,
	failed.bsc_6
	)

#Remove objects to free memory
rm(failed.bsc_0)
rm(failed.bsc_1)
rm(failed.bsc_2)
rm(failed.bsc_3)
rm(failed.bsc_4)
rm(failed.bsc_5)
rm(failed.bsc_6)

success.bsc_0 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_0_0.csv', sep = ''))
success.bsc_1 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_1_0.csv', sep = ''))
success.bsc_2 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_2_0.csv', sep = ''))
success.bsc_3 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_3_0.csv', sep = ''))
success.bsc_4 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_4_0.csv', sep = ''))
success.bsc_5 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_5_0.csv', sep = ''))
success.bsc_6 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_6_0.csv', sep = ''))

success.bsc <- rbind(
	success.bsc_0,
	success.bsc_1,
	success.bsc_2,
	success.bsc_3,
	success.bsc_4,
	success.bsc_5,
	success.bsc_6
	)

rm(success.bsc_0)
rm(success.bsc_1)
rm(success.bsc_2)
rm(success.bsc_3)
rm(success.bsc_4)
rm(success.bsc_5)
rm(success.bsc_6)

pdf('Figure5.pdf', width = 20, height = 5)
	par(mfrow=c(1,2))
	plot(density(na.omit(failed.bsc$tVal), n = 2000, adjust = 4),
		xlim = c(-7, 7),
		main = '',
		lwd = 3,
		xaxt = "n",
		xlab = paste('t-values'),
		las = 1)
	abline(v = -2, lty = 2, col = 'grey')
	abline(v = 2, lty = 2, col = 'grey')
	abline(v = 0, lty = 1)
	axis(1, at = c(-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10), label = c(-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10))
	title(expression(paste('Failed Coups (', phi, ')')))

	plot(density(na.omit(success.bsc$tVal), n = 2000, adjust = 4),
		xlim = c(-7, 7),
		main = '',
		lwd = 3,
		xaxt = "n",
		xlab = 't-values',
		las = 1)
	abline(v = -2, lty = 2, col = 'grey')
	abline(v = 2, lty = 2, col = 'grey')
	abline(v = 0, lty = 1)
	axis(1, at = c(-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10), label = c(-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10))
	title(expression(paste('Successful Coups (', psi, ')')))
dev.off()



#----------------------------------------------------
#Table A5
#----------------------------------------------------
failed.bsc <- subset(failed.bsc, !is.na(pVal))
cf.shnegtot.fl.bsc <- sum(failed.bsc$Coef < 0)/nrow(failed.bsc)
cf.shnegsig.fl.bsc <- sum(failed.bsc$Coef < 0 & failed.bsc$pVal < 0.05)/nrow(failed.bsc)
cf.shpostot.fl.bsc <- sum(failed.bsc$Coef > 0)/nrow(failed.bsc)
cf.shpossig.fl.bsc <- sum(failed.bsc$Coef > 0 & failed.bsc$pVal < 0.05)/nrow(failed.bsc)
cf.average.fl.bsc <- mean(failed.bsc$Coef)
se.average.fl.bsc <- mean(failed.bsc$SE)
tv.average.fl.bsc <- mean(failed.bsc$tVal)

success.bsc <- subset(success.bsc, !is.na(pVal))
cf.shnegtot.sc.bsc <- sum(success.bsc$Coef < 0)/nrow(success.bsc)
cf.shnegsig.sc.bsc <- sum(success.bsc$Coef < 0 & success.bsc$pVal < 0.05)/nrow(success.bsc)
cf.shpostot.sc.bsc <- sum(success.bsc$Coef > 0)/nrow(success.bsc)
cf.shpossig.sc.bsc <- sum(success.bsc$Coef > 0 & success.bsc$pVal < 0.05)/nrow(success.bsc)
cf.average.sc.bsc <- mean(success.bsc$Coef)
se.average.sc.bsc <- mean(success.bsc$SE)
tv.average.sc.bsc <- mean(success.bsc$tVal)

eba.out.table.fl <- cbind(
	'Failed Coup (phi)',
	deci(cf.shnegtot.fl.bsc, 3),
	deci(cf.shnegsig.fl.bsc, 3),
	deci(cf.shpostot.fl.bsc, 3),
	deci(cf.shpossig.fl.bsc, 3),
	deci(cf.average.fl.bsc, 2),
	deci(se.average.fl.bsc, 2),
	deci(tv.average.fl.bsc, 2)
	)
#
eba.out.table.sc <- cbind(
	'Successful Coup (psi)',
	deci(cf.shnegtot.sc.bsc, 3),
	deci(cf.shnegsig.sc.bsc, 3),
	deci(cf.shpostot.sc.bsc, 3),
	deci(cf.shpossig.sc.bsc, 3),
	deci(cf.average.sc.bsc, 2),
	deci(se.average.sc.bsc, 2),
	deci(tv.average.sc.bsc, 2)
	)
#
the_header <- cbind(
	c('Variable', '', ''),
	c('Prop. of Coef.', 'Negative', ''),
	c('Prop. of Coef.', 'Negative and', 'p<0.05'),
	c('Prop. of Coef.', 'Positive', ''),
	c('Prop. of Coef.', 'Positive and', 'p<0.05'),
	c('Mean Coefficient', '', ''),
	c('Mean SE', '' ,''),
	c('Mean t-Value', '' ,'')
	)


library(xtable)
TableA5 <- print(
			 xtable(rbind(the_header, eba.out.table.fl, eba.out.table.sc),
			 align = 'l|l|cc|cc|ccc|',
			  label = 'ext:bnds'),
		 include.rownames = FALSE,
		 include.colnames = FALSE,
		 caption.placement = 'top',
		 table.placement = "H",
		 hline.after = c(0,0, 3, 3, 4, 5, 5),
		 floating = FALSE
		 )
